package business.web.controller;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;

import business.transcation.AlarmUserQueryCommand;

import com.fasterxml.jackson.databind.ObjectMapper;

import core.query.FilterFactory;
import core.query.Pagination;
import core.query.QueryParam;
import core.query.QueryRule;

/**
 * Servlet implementation class AlarmUserController
 */
public class AlarmUserController extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public AlarmUserController() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String queryParams = request.getParameter("queryParams");
		request.setAttribute("queryParams", queryParams);
//		QueryContext queryContext = new QueryContext();
		QueryParam param = new QueryParam();
		if (StringUtils.isNotBlank(queryParams)) {
			ObjectMapper mapper = new ObjectMapper();
			param = mapper.readValue(queryParams, QueryParam.class);
			for (QueryRule rule : param.getRules()) {
				param.addFilter(FilterFactory.createFilter(rule));
			}
		}
		String page = request.getParameter("page");
		if (StringUtils.isBlank(page)) {
			page = "1";
		}
		
//		QueryContext queryMap = new QueryContext();
//		String quickFilter = request.getParameter("_fliterType");
//		if (StringUtils.isNotBlank(quickFilter) && "_quickFilter".equals(quickFilter)) {
//			String filterValue = request.getParameter("quickFilter");
////			QuickFilter filter = new QuickFilter();
////			filter.setFilterValue(filterValue);
////			filter.bindKeyword("alarm_user_code");
////			filter.bindKeyword("alarm_user_name");
//			List<ISqlFilter> filterList = new ArrayList<ISqlFilter>();
//			filterList.add(new ContainFliter("alarm_user_code", filterValue));
//			filterList.add(new RightLikeFilter("alarm_user_name", filterValue));
//			filterList.add(new LeftLikeFilter("alarm_user_name", filterValue));
//			queryMap.put("filterList", filterList);
//			queryMap.put("logic", "or");
//		}
		Pagination pagination = Pagination.newInstance(Integer.parseInt(page));
		param.setPagination(pagination);
		try {
			request.setAttribute("queryContext", new AlarmUserQueryCommand().execute(param));
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		request.getRequestDispatcher("/alarm_user_list.vm").forward(request,
				response);
	}

}
